<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>扫商品入库</title>
    <link rel="stylesheet" href="/sb/stock.css"/>
    <link rel="stylesheet" href="/layui/css/layui.css" media="all">
    <style type="text/css">
        .record-box {
            position: relative;
            width: 700px;
            padding: 0 5px 20px;
            font-size: 13px;
            color: #787878;
        }

        .record-box h2 {
            font-weight: bold;
            font-size: 18px;
            margin-bottom: 40px;
        }

        .record-input, .record-tips {
            width: 320px;
            margin: 0 auto;
            text-align: left;
        }

        .record-input input[type=text] {
            width: 400px;
            height: 22px;
            line-height: 22px;
            font-size: 16px;
            padding: 14px 9px;
            border: 1px solid #787878;
        }

        .record-tips {
            padding: 5px 0;
            color: #f00;
            font-size: 13px;
            line-height: 2;
        }
    </style>
    <script src="/js/jquery-3.3.1.min.js"></script>
    <script>
        var index = 0;
        try {
            index = parent.layer.getFrameIndex(window.name); //获取窗口索引
        } catch (e) {
            index = 0;
        }

        $(function () {
            // var number = $("#code").val();
            // if (number != '') {
            //     getSpec(number);
            // }
            $("#code").focus();

            // //扫码表单提交事件
            // $("#getGoodsSpecForm").submit(function () {
            //     var number = $("#code").val();
            //     if (number == '') {
            //         $("#record-tips").html("请输入商品编码");
            //         return false;
            //     } else {
            //         //AJAX查询是否存在
            //         getSpec(number);
            //
            //         return false;
            //     }
            // })
            //
            // //获取商品规格
            // function getSpec(number) {
            //     $.ajax({
            //         url: "/ajax_stock_in/goods_spec_by_iid_and_number",
            //         type: "POST",
            //         dataType: "json",
            //         contentType: "application/json",
            //         data: JSON.stringify({specNumber: number, invoiceId: '[[${iid}]]'}),
            //         success: function (res) {
            //             // console.log(res);
            //             if (res.code == 0) {
            //                 $("#record-tips").html("");
            //                 var goodsShowName = res.data.goodsName;
            //                 goodsShowName+= '  颜色：'+res.data.colorValue;
            //                 goodsShowName+= ' 尺码：'+res.data.sizeValue;
            //                 goodsShowName+= ' SKU：'+res.data.specNumber;
            //                 $("#goodsName").text(goodsShowName);
            //                 $("#goodsId").val(res.data.goodsId);
            //                 $("#invoiceInfoId").val(res.data.invoiceInfoId);
            //                 $("#goodsNumber").val(res.data.goodsNumber);
            //                 $("#specId").val(res.data.specId);
            //                 $("#specNumber").val(res.data.specNumber);
            //                 // $("#locationId").val(res.data.locationId);
            //                 $("#quantity").val(res.data.qualifiedQuantity - res.data.inQuantity);
            //                 $("#stockIn").show();
            //             } else {
            //                 $("#stockIn").hide();
            //                 $("#invoiceInfoId").val("0");
            //                 $("#goodsName").text("");
            //                 $("#goodsId").val("0");
            //                 $("#goodsNumber").val("");
            //                 $("#specId").val("0");
            //                 $("#specNumber").val("");
            //                 // $("#locationId").val("");
            //                 $("#quantity").val("0");
            //                 $("#record-tips").html("没有找到该商品信息");
            //             }
            //
            //             // $("#code").val("");
            //
            //             // alert(JSON.stringify(res));
            //         }
            //     });
            // }


        });

    </script>
</head>
<body>
<blockquote class="layui-elem-quote layui-text">
    商品入库
</blockquote>


<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend></legend>
</fieldset>
<form  action="/stock_in/purchase_in_qrcode" method="get">
    <input type="hidden" name="iid" th:value="${iid}"/>
<div class="layui-form-item">
    <div class="layui-input-block">
        <input type="text" id="code" name="number" lay-verify="code" th:value="${number}" autocomplete="off"
               placeholder="商品编码" class="layui-input"/>
    </div>
    <div>
        <input type="submit" value="" />
    </div>
</div>
</form>

<form class="layui-form" action="">
    <input type="hidden" id="invoiceInfoId" name="invoiceInfoId"  th:value="${invoiceInfo.getId()}"/>
    <input type="hidden" id="invoiceId" name="invoiceId" th:value="${iid}"/>
    <input type="hidden" id="goodsId" name="goodsId" th:value="${invoiceInfo.getGoodsId()}"/>
    <input type="hidden" id="goodsNumber" name="goodsNumber" th:value="${invoiceInfo.getGoodsNumber()}"/>
    <input type="hidden" id="specId" name="specId" th:value="${invoiceInfo.getSpecId()}"/>
    <input type="hidden" id="specNumber" name="specNumber" th:value="${invoiceInfo.getSpecNumber()}"/>
    <input type="hidden" id="quantity" name="quantity" placeholder="默认采购数" th:value="${invoiceInfo.qualifiedQuantity - invoiceInfo.inQuantity}"/>

    <div class="layui-form-item">
        <div class="record-tips" id="record-tips">[[${msg}]]</div>
    </div>
    <div class="T-box" id="stockIn" style="margin-top: 20px;display: none;">
    </div>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label">商品</label>-->
<!--            <div class="layui-input-block">-->
<!--            <span id="goodsName"></span>-->
<!--            </div>-->
<!--        </div>-->
        <div class="layui-form-item">
            <label class="layui-form-label">入库位置</label>
            <div class="layui-input-inline">
                <select id="locationId" name="locationId" lay-verify="required" lay-filter="locationId">
                    <option value="">请选择仓库</option>
                    <option th:each="item:${houses}" th:value="${item.getId()}" th:text="${item.getName()}">深圳仓</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select id="reservoirId" name="reservoirId" lay-verify="required" lay-filter="reservoirId" >
                    <option value="">请选库区</option>
                    <option th:each="item:${reservoir}" th:value="${item.getId()}" th:text="${item.getName()}">深圳仓</option>
                </select>
            </div>
            <div class="layui-input-inline">
                <select id="shelfId" name="shelfId" lay-verify="required" lay-search="">
                    <option value="">请选择仓位</option>
                    <option th:each="item:${shelf}" th:value="${item.getId()}" th:text="${item.getName()}">深圳仓</option>
                </select>
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
        </div>



    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">确认入库</button>
        </div>
    </div>
</form>

<script src="/layui/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    $(function () {


    layui.use(['form','layer' ], function () {
        var form = layui.form
            , layer = layui.layer;
        // var locationId = [[${locationId}]];
        $("#locationId").val([[${locationId}]]);
        // form.render('select');
        $("#reservoirId").val([[${reservoirId}]]);
        // form.render('select');
        $("#shelfId").val([[${shelfId}]]);
        form.render('select');

        //自定义验证规则
        // form.verify({
        //     code: function (value) {
        //         if (value.length < 5) {
        //             return '标题至少得5个字符啊';
        //         }
        //     }
        //     , pass: [
        //         /^[\S]{6,12}$/
        //         , '密码必须6到12位，且不能出现空格'
        //     ]
        //     , content: function (value) {
        //         layedit.sync(editIndex);
        //     }
        // });

        //监听提交
        form.on('submit(demo1)', function (data) {
            console.log(data.field);

            if (data.field.invoiceInfoId == '' || data.field.invoiceInfoId == 0) {
                alert("数据错误，请关闭页面重新操作");
                return false;
            }
            if (data.field.goodsId == '' || data.field.goodsId == '0' || data.field.specId == '' || data.field.specId == '0' || data.field.goodsNumber == '' || data.field.specNumber == '') {
                alert("入库的商品不存在");
                return false;
            }
            if (data.field.locationId == '' || data.field.locationId == 0 || data.field.locationId == null) {
                alert('请填选择入库仓库');
                return false;
            }
            if (data.field.reservoirId == '' || data.field.reservoirId == 0 || data.field.reservoirId == null) {
                alert('请填选择入库库区');
                return false;
            }
            if (data.field.shelfId == '' || data.field.shelfId == 0 || data.field.shelfId == null) {
                alert('请填选择入库仓位');
                return false;
            }
            // var dataJson = {
            //     invoiceId: [[${iid}]],
            //     invoiceInfoId: invoiceInfoId,
            //     goodsId: goodsId,
            //     goodsNumber: goodsNumber,
            //     specId: specId,
            //     specNumber: specNumber,
            //     locationId: locationId,
            //     qualifiedQuantity: qualifiedQuantity,
            //     reservoirId: reservoirId,
            //     shelfId: shelfId
            // };


            // layer.alert(JSON.stringify(data.field), {
            //     title: '最终的提交信息'
            // });

            $.ajax({
                url: "/ajax_stock_in/goods_in",
                type: "POST",
                dataType: "json",
                contentType: "application/json",
                data: JSON.stringify(data.field),
                success: function (res) {
                    if (res.code == 0) {
                        alert("入库成功");
                        try {
                            // parent.shelf(locationName + "  " + reservoirName + "  " + shelfName);
                            parent.layer.close(index);
                            parent.location.reload();
                        } catch (e) {
                        }

                    } else {
                        alert(res.msg);
                    }
                }
            });

            return false;
        });

        //仓库下拉值变动
        form.on('select(locationId)', function(data){
            console.log(data);
            var locationId= data.value;
            $.ajax({
                type: "POST",
                url: "/ajax_stock_in/get_reservoir_by_locationId",
                data: locationId,
                dataType: 'json',
                contentType: 'application/json',
                success: function (data) {
                    var str = "<option value = '0'>请选择</option>";
                    var data1 = data.data;
                    for (var i = 0; i < data1.length; i++) {
                        str = str + "<option value = '" + data1[i].id + "'>" + data1[i].name + "</option>";
                    }
                    $("#reservoirId").html(str);
                    $("#shelfId").contents().remove();
                    form.render('select');
                }
            });
        });

        //库区值变动
        form.on('select(reservoirId)', function(data) {
            var reservoirId = data.value;
            $.ajax({
                type: "POST",
                url: "/ajax_stock_in/get_shelf_by_reservoirId",
                data: reservoirId,
                dataType: 'json',
                contentType: 'application/json',
                success: function (data) {
                    var str = "<option value = ''>直接选择或搜索选择</option>";
                    var data1 = data.data;
                    for (var i = 0; i < data1.length; i++) {
                        str = str + "<option value = '" + data1[i].id + "'>" + data1[i].name + "</optiom>";
                    }
                    $("#shelfId").html(str);
                    form.render('select');
                }
            });
        });


        //表单赋值
        // layui.$('#LAY-component-form-setval').on('click', function () {
        //     form.val('example', {
        //         "username": "贤心" // "name": "value"
        //         , "password": "123456"
        //         , "interest": 1
        //         , "like[write]": true //复选框选中状态
        //         , "close": true //开关状态
        //         , "sex": "女"
        //         , "desc": "我爱 layui"
        //     });
        // });


    });

    })
</script>



</body>
</html>